package logic

import (
	"context"
	"fmt"
	"good_srv/good"
	"good_srv/internal/svc"
	"good_srv/models"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品发布
func (l *GoodAddLogic) GoodAdd(in *good.GoodAddRequest) (*good.GoodAddResponse, error) {
	// todo: add your logic here and delete this line
	goods := models.Goods{
		UserId: int(in.UserId),
		Title:  in.Title,
		Image:  in.Image,
		Price:  float64(in.Price),
		Stock:  in.Stock,
		Status: in.Status,
	}
	if err := goods.GoodAdd(); err != nil {
		logx.Error("商品添加失败")
		return nil, fmt.Errorf("商品添加失败")
	}
	return &good.GoodAddResponse{
		Id: int64(goods.ID),
	}, nil
}
